package softtech.app.secretcaller;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.ContentValues;

public class DatabaseContact {
	final String TAG = "DATABASE CONTACT";
	public static final String TABLE_NAME = "Contacts";
	public static final String KEY_ID = "contact_id";
	public static final String KEY_CONTACT_NAME = "contact_name";
	public static final String KEY_PHONE = "phone_number";
	public static final String KEY_AVATAR = "_avatar";
	public static final String KEY_LAST_CALL = "_last_call";
	public static final String KEY_LAST_SMS = "_last_sms";
	public static final String KEY_IS_SYNC = "_is_sync";
	public static final String KEY_ID_USER = "user_id";
	public static final String KEY_PRIMARY = "id";

	private int _Id;
	private String mContactName;
	private String mPhoneNumber;
	private String mAvatar;
	private String mLastCall;
	private String mLastSms;
	private int isSync;
	private int _User_Id;
	private long mPrimaryKey;

	public DatabaseContact() {
		this(-1, "", "");
	}

	public DatabaseContact(int id, String cName, String cPhone) {
		this(id, cName, cPhone, -1);

	}

	public DatabaseContact(int id, String cName, String cPhone, int uId) {
		_Id = id;
		mContactName = cName;
		mPhoneNumber = cPhone;
		mLastCall = "";
		mLastSms = "";
		mAvatar = "";
		isSync = -1;
		_User_Id = uId;
	}

	public int get_Id() {
		return _Id;
	}

	public void set_Id(int _Id) {
		this._Id = _Id;
	}

	public String getContactName() {
		return mContactName;
	}

	public void setContactName(String mContactName) {
		this.mContactName = mContactName;
	}

	public String getPhoneNumber() {
		return mPhoneNumber;
	}

	public void setPhoneNumber(String mPhoneNumber) {
		this.mPhoneNumber = mPhoneNumber;
	}

	public String getAvatar() {
		return mAvatar;
	}

	public void setAvatar(String mAvatar) {
		this.mAvatar = mAvatar;
	}

	public String getLastCall() {
		return mLastCall;
	}

	public void setLastCall(String mLastCall) {
		this.mLastCall = mLastCall;
	}

	public String getLastSms() {
		return mLastSms;
	}

	public void setLastSms(String mLastSms) {
		this.mLastSms = mLastSms;
	}

	public int getIsSync() {
		return isSync;
	}

	public void setIsSync(int isSync) {
		this.isSync = isSync;
	}

	public int get_User_Id() {
		return _User_Id;
	}

	public void set_User_Id(int _User_Id) {
		this._User_Id = _User_Id;
	}
	
	public long getPrimaryKey() {
		return mPrimaryKey;
	}

	public void setPrimaryKey(long mPrimaryKey) {
		this.mPrimaryKey = mPrimaryKey;
	}

	/**
	 * Port Database Contact to JSON Object
	 * 
	 * @return this object to json object
	 */
	public JSONObject toJSONObject() {
		JSONObject jsonObject = new JSONObject();
		try {
			if (_Id != -1) {
				jsonObject.put(KEY_ID, _Id);
				jsonObject.put(KEY_CONTACT_NAME, mContactName);
				jsonObject.put(KEY_PHONE, mPhoneNumber);

				if (ToolsSaveFile.checkStringNotNull(mAvatar, 4)) {
					jsonObject.put(KEY_AVATAR, mAvatar);
				}

				if (ToolsSaveFile.checkStringNotNull(mLastCall, 4)) {
					jsonObject.put(KEY_LAST_CALL, mLastCall);
				}

				if (ToolsSaveFile.checkStringNotNull(mLastSms, 4)) {
					jsonObject.put(KEY_LAST_SMS, mLastSms);
				}

				jsonObject.put(KEY_IS_SYNC, isSync);

				jsonObject.put(KEY_ID_USER, _User_Id);

			}else{
				jsonObject.put(KEY_ID, _Id);
				jsonObject.put(KEY_CONTACT_NAME, mContactName);
				jsonObject.put(KEY_PHONE, mPhoneNumber);

				if (ToolsSaveFile.checkStringNotNull(mAvatar, 4)) {
					jsonObject.put(KEY_AVATAR, mAvatar);
				}

				if (ToolsSaveFile.checkStringNotNull(mLastCall, 4)) {
					jsonObject.put(KEY_LAST_CALL, mLastCall);
				}

				if (ToolsSaveFile.checkStringNotNull(mLastSms, 4)) {
					jsonObject.put(KEY_LAST_SMS, mLastSms);
				}

				jsonObject.put(KEY_IS_SYNC, isSync);

				jsonObject.put(KEY_ID_USER, _User_Id);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonObject;
	}

	/**
	 * Port Database Contact to String of JSON Object
	 * 
	 * @return this object to json object string
	 */
	public String toJSONString() {
		return toJSONObject().toString();
	}

	/**
	 * Convert database contact to ContentValue
	 * 
	 * @return
	 */
	public ContentValues toContentValue() {
		ContentValues values = new ContentValues();
		values.put(KEY_ID, _Id);
		values.put(KEY_CONTACT_NAME, mContactName);
		values.put(KEY_PHONE, mPhoneNumber);

		if (ToolsSaveFile.checkStringNotNull(mAvatar, 4)) {
			values.put(KEY_AVATAR, mAvatar);
		}

		if (ToolsSaveFile.checkStringNotNull(mLastCall, 4)) {
			values.put(KEY_LAST_CALL, mLastCall);
		}

		if (ToolsSaveFile.checkStringNotNull(mLastSms, 4)) {
			values.put(KEY_LAST_SMS, mLastSms);
		}

		values.put(KEY_IS_SYNC, isSync);

		values.put(KEY_ID_USER, _User_Id);

		return values;
	}

	/**
	 * Convert database contact to ContentValue last call
	 * 
	 * @return
	 */
	public ContentValues toLastCallContentValue() {
		ContentValues values = new ContentValues();
		
		if (ToolsSaveFile.checkStringNotNull(mLastCall, 4)) {
			values.put(KEY_LAST_CALL, mLastCall);
		}

		if (ToolsSaveFile.checkStringNotNull(mLastSms, 4)) {
			values.put(KEY_LAST_SMS, mLastSms);
		}

		return values;
	}
	
	
	/**
	 * Convert database contact object to list of namevaluepair
	 */
	public List<NameValuePair> toListNameValuePair() {
		List<NameValuePair> listPair = new ArrayList<NameValuePair>();
		listPair.add(new BasicNameValuePair("id", Integer.toString(_Id)));
		listPair.add(new BasicNameValuePair(KEY_CONTACT_NAME, mContactName));
		listPair.add(new BasicNameValuePair(KEY_PHONE, mPhoneNumber));

		if (ToolsSaveFile.checkStringNotNull(mAvatar, 4)) {
			listPair.add(new BasicNameValuePair(KEY_AVATAR, mAvatar));

		}

		if (ToolsSaveFile.checkStringNotNull(mLastCall, 4)) {
			listPair.add(new BasicNameValuePair(KEY_LAST_CALL, mLastCall));

		}

		if (ToolsSaveFile.checkStringNotNull(mLastSms, 4)) {
			listPair.add(new BasicNameValuePair(KEY_LAST_SMS, mLastSms));
		}

		listPair.add(new BasicNameValuePair(KEY_IS_SYNC, Integer
				.toString(isSync)));

		listPair.add(new BasicNameValuePair(KEY_ID_USER, Integer
				.toString(_User_Id)));

		return listPair;
	}

}
